<template>
  <div id="grxx">
   <h3>个人信息</h3>
    <el-container>
      <el-main>
        <el-form :model="ruleForm" label-position="left" label-width="100px" style="margin: 20px">
          <el-form-item label="个人头像:" prop="imageUrl">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8080/sysUsers/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img v-if="ruleForm.avatarImg" :src="ruleForm.avatarImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="昵称:" prop="nicheng" style="width: 50%;">
            <el-input v-model="ruleForm.nickName"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱:" prop="email" style="width: 50%;">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号:" prop="phoneNumber" style="width: 50%;">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="用户姓名:" prop="name" style="width: 50%;">
            <el-input v-model="ruleForm.realName"></el-input>
          </el-form-item>
          <el-form-item label="生日:" prop="birth" style="width: 500px;">
            <el-date-picker
                v-model="ruleForm.birth"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>

<!--          <el-form-item label="省/市/区:" prop="selector1" label-width="100px" :inline="true">-->
<!--            <el-cascader size="large" :options="pcaTextArr" v-model="ruleForm.selectedOptions" @change="handleChangeAddress">-->
<!--            </el-cascader>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="详细地址:" prop="detailedAddress" style="width: 50%;">-->
<!--            <el-input type="textarea" v-model="ruleForm.detailedAddress"></el-input>-->
<!--          </el-form-item>-->

          <el-form-item>
            <el-button type="primary" @click="upd('ruleForm')" style="width: 150px;line-height: 15px;">修改</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
   </div>
</template>

<script>
//省市区
import { pcaTextArr } from "element-china-area-data";
export default {
  name: 'grxx',
  components: {
  },
  data() {
    return {
      pcaTextArr, //地址数据
      selectedOptions: [], //地址绑定,必须是数组
      ary: '',
      imageUrl: '',
      ruleForm: {
        avatarImg: '',
        phoneNumber: '',
        storeType: '',
        selectedOptions: [],
        // selector1: '',
        detailedAddress: '',
        birth:'',
        name:'',
        nicheng:'',
        email:''
      },
      user: JSON.parse(localStorage.getItem("user")),
    }
  },
  methods: {
    //个人信息修改
    async upd(){
      const  {data:res} =await this.$http.post(`/sysUsers/sysUserUpd/`,this.ruleForm)
      this.$message.success('修改成功')
      //自定义事件处理界面刷新
      this.$emit('queryUser');
    },
    //拿到id
     async getsysId() {
       //拿用户id
       this.sysUsersId=this.$store.state.user.sysUsersId
       const {data: res} = await this.$http.post(`/sysUsers/sysUsersById/${this.sysUsersId}`)
       this.ruleForm = res
       console.log(res)
       // console.log("查询到的用户表单数据",this.ruleForm)
    },
    //头像成功上传的回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = res;
      this.ruleForm.avatarImg = this.imageUrl;
    },
    // 上传之前校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    //级联地区改变
    handleChangeAddress(value) {
      console.log(value.join('-'));
    },
  },
  created() {
    let temp = this.ary.split('-')
    console.log('ary', temp);
    this.selectedOptions = temp
    //初始化执行方法
    this.getsysId()
  }
}
</script>

<style>
/* 图片上传 */
.avatar-uploader .el-upload {
  border: 1px dashed black;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 30px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  color: black;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}



</style>
